import { reqlogin, requserinfo } from '@/api/user'
import { setToken, getToken, removeToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userinfo: {} // 将获取的用户信息存在vuex中
  },
  mutations: {
    setuserToken(state, payload) {
      state.token = payload
      setToken(payload)
    },
    // 修改
    setinfo(state, payload) {
      state.userinfo = payload
    },
    // 退出
    logout(state) {
      state.token = null
      removeToken()
      state.userinfo = {}
    }
  },
  actions: {
    // 在这边发生请求
    async login({ commit }, payload) {
      const { data } = await reqlogin(payload)
      // console.log(data)
      commit('setuserToken', data)
    },
    // 发起获取用户资料请求
    async getIfo({ commit }) {
      const { data } = await requserinfo()
      console.log(data)
      commit('setinfo', data)
    }
  }
}

